package com.atguigu.flink.chapter05.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class DataGenDemo1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        DataGeneratorSource<String> source = new DataGeneratorSource<>(new GeneratorFunction<Long, String>() {
            @Override
            public String map(Long value) throws Exception {
                return "atguigu" + value;
            }
        }, Long.MAX_VALUE,
                RateLimiterStrategy.perSecond(1),
                Types.STRING);


        DataStreamSource<String> stream = env.fromSource(source,
                WatermarkStrategy.noWatermarks(),
                "data-source");

        stream.print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


//public class DataGenDemo1 {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",20000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        DataGeneratorSource<String> source = new DataGeneratorSource<>(new GeneratorFunction<Long, String>() {
//            @Override
//            public String map(Long value) throws Exception {
//                return "atguigu" + value;
//            }
//        }, Long.MAX_VALUE,
//                RateLimiterStrategy.perSecond(1),
//                Types.STRING);
//
//        DataStreamSource<String> stream = env.fromSource(source,
//                WatermarkStrategy.noWatermarks(),
//                "data-source");
//
//        stream.print();
//
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}


//public class DataGenDemo1 {
//    public static void main(String[] args)  {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",20000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        DataGeneratorSource<String> source = new DataGeneratorSource<>(new GeneratorFunction<Long, String>() {
//            @Override
//            public String map(Long value) throws Exception {
//                return "atguigu" + value;
//            }
//        }, Long.MAX_VALUE,
//                RateLimiterStrategy.perSecond(1),
//                Types.STRING);
//
//        DataStreamSource<String> stream = env.fromSource(source,
//                WatermarkStrategy.noWatermarks(),
//                "data-source");
//
//        stream.print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}